package com.yoomiito.app.ui.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.rxbus.RxBus;
import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.Constant;
import com.yoomiito.app.R;
import com.yoomiito.app.base.WebViewActivity;
import com.yoomiito.app.present.account.BindingPhonePresent;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.CheckUtil;
import com.yoomiito.app.utils.Const;
import com.yoomiito.app.utils.EditTextUtil;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.RxBusManager;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.SelectView;


import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * 微信授权后绑定手机号码
 */
public class BindingPhoneActivity extends BaseActivity<BindingPhonePresent> {

    @BindView(R.id.act_bind_phone_send_code)
    Button sendCode;
//    @BindView(R.id.tv_protocol)
//    TextView tvProtocol;
    @BindView(R.id.tv_phone)
    EditText mEditText;
    @BindView(R.id.iv_delete)
    ImageView mImageView;
    @BindView(R.id.checkView)
    SelectView checkView;

    private String mPhone;
    private EditTextUtil mEditTextUtil;
    private int type;
    private String mGuiMiHuiId;

    public static void start(Context context){
        Intent intent = new Intent(context,BindingPhoneActivity.class);
        context.startActivity(intent);
    }

    @Override
    public BindingPhonePresent newP() {
        return new BindingPhonePresent(App.mAppConfig);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_binding_phone;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        type = getIntent().getIntExtra(Constant.KEY,-1);
        mGuiMiHuiId = getIntent().getStringExtra(Constant.DATA);
        mEditTextUtil = new EditTextUtil(mEditText, sendCode);
        mEditTextUtil.setNumberLength(11);
        mEditTextUtil.setIvDelete(mImageView);

//        RxTextView.textChanges(mEditText)
//                .debounce(600, TimeUnit.MILLISECONDS )
//                .map(new Function<CharSequence, String>() {
//                    @Override
//                    public String apply(CharSequence charSequence) throws Exception {
//                        if (charSequence.length()>4) return charSequence.toString();
//                        return "";
//                    }
//                })

    }


    @Override
    protected void onResume() {
        super.onResume();
        mEditText.setText("");
        mEditTextUtil.setEditTextListener();
    }

    /**
     * 去往---> 确认验证码
     */
    public void toPhoneCodeCheckActivity() {
        mEditTextUtil.deleteTextWatcher();
        if (type== AllCode.GuiMiHui){
            Router.newIntent(context)
                    .to(InputCodeCheckActivity.class)
                    .putString("phone", mPhone)
                    .putInt("type",type)
                    .putString(Constant.DATA, mGuiMiHuiId)
                    .launch();
            return;
        }
        Router.newIntent(context)
                .to(InputCodeCheckActivity.class)
                .putString("phone", mPhone)
                .putInt("type", Constant.WX_Login)
                .launch();
    }

    /**
     * 删除了一个R.id.tv_protocol
     *
     * @param view
     */
    @OnClick({R.id.act_bind_phone_send_code, R.id.iv_delete, R.id.tv_protocol,R.id.privacyAgreementTv,})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_delete://删除
                mEditText.setText("");
                mImageView.setVisibility(View.GONE);
                sendCode.setEnabled(false);
                break;
            case R.id.act_bind_phone_send_code:
                mPhone = mEditText.getText().toString().trim();
                if (TextUtils.isEmpty(mPhone)){
                    getvDelegate().toastShort(getString(R.string.input_phone_num));
                    return;
                }
                if (!CheckUtil.isMobileNO(mPhone)) {
                    getvDelegate().toastShort("请输入正确的11位手机号吗");
                    return;
                }
                if (!checkView.isSelected()){
                    YMToast.showCenterToast("请阅读并同意《用户协议》和《隐私政策》");
                    return;
                }
                getP().requestSendCode(mPhone);
                break;
            case R.id.tv_protocol://用户协议
                Router.newIntent(this)
                        .to(WebViewActivity.class)
                        .putInt(Constant.TYPE, AllCode.UserAgreement)
                        .launch();
                break;
            case R.id.privacyAgreementTv://隐私协议
                Router.newIntent(this)
                        .to(WebViewActivity.class)
                        .putInt(Constant.TYPE, Const.Url.PrivacyAgreement)
                        .launch();
                break;
        }
    }

    @Override
    public void eventMessage(EventMessage eventMessage) {
        super.eventMessage(eventMessage);
        if (AllCode.LoginSuccess.equals(eventMessage.getEventType())){
            this.finish();
        }
    }

    @Override
    protected void onDestroy() {
        mEditTextUtil.deleteTextWatcher();
//        RxBusManager.unregisterRxBusManager(this);
        super.onDestroy();
    }
}
